// $Id: AuthenticationLayer.nc,v 1.6 2008/06/24 05:32:31 regehr Exp $

/*									tab:4
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */
 
#include "Timer.h"
#include "Hook.h"

#define NUM_NODES     22
#define HEADER 	0x08 
/**
 * Implementation of the RadioCountToLeds application. RadioCountToLeds 
 * maintains a 4Hz counter, broadcasting its value in an AM packet 
 * every time it gets updated. A RadioCountToLeds node that hears a counter 
 * displays the bottom three bits on its LEDs. This application is a useful 
 * test to show that basic AM communication and timers work.
 *
 * @author Philip Levis
 * @date   June 6 2005
 */

module RadioCountToLedsC @safe() {
  uses {
    interface Leds;
    interface Boot;
    interface Receive;
    interface AMSend;
    interface Timer<TMilli> as MilliTimer;
    interface Timer<TMilli> as FloodTimer;
    interface SplitControl as AMControl;
    interface Packet;
    interface AMPacket;
  }
}
implementation {

 	message_t packet;
  	bool locked;
	uint16_t interv = 0 ;
	uint16_t temp = 0;
	uint16_t pres = 0;
	uint16_t hum  = 0;


  event void Boot.booted() {
    call AMControl.start();
  }

  event void AMControl.startDone(error_t err) {
    if (err == SUCCESS) {
      	call MilliTimer.startPeriodic(2000);
    }
    else {
      call AMControl.start();
    }
  }

  event void AMControl.stopDone(error_t err) {
    // do nothing
  }

  event void FloodTimer.fired() {
	// do nothing for this test

} 
  event void MilliTimer.fired() {
	am_addr_t src=call AMPacket.address();
	am_addr_t dest;

	
	temp = (temp + 1) % 100;
	pres = temp * 10;
	hum  = temp + 100;
	
    if (locked) {
      return;
    }
    else {
	
      hook_msg_t* hm = (hook_msg_t*)call Packet.getPayload(&packet, sizeof(hook_msg_t));
	
      if (hm == NULL) {
	return;
      }
	
    	hm->msg[0] = temp;
	hm->msg[1] = pres;
	hm->msg[2] = hum;
	hm->header = HEADER;
	hm->size = 3;
	dest = (src + 1) % NUM_NODES;


	/* intrusion of type internal node sends wrong network credentials*/
	if((src==2)&&(interv <= 30))
               { 
		dest = 1; 
		interv = interv + 2;
		}
         
	if ((src == 2)&&(interv == 32))
                {
			dest = 1;
			hm->size = 5 ;
              }



	if (call AMSend.send(dest, &packet, sizeof(hook_msg_t)) == SUCCESS) {
			
			locked = TRUE;//AM_BROADCAST_ADDR
	      	}
    }
	
  }

  event message_t* Receive.receive(message_t* bufPtr, 
				   void* payload, uint8_t len) {
	
    if (len != sizeof(hook_msg_t)) {return bufPtr;}
    else {
        hook_msg_t* hm = (hook_msg_t*)payload;
	am_addr_t src=call AMPacket.source(bufPtr);
	am_addr_t dest=call AMPacket.address();
	
       return bufPtr;
    }
  }

  event void AMSend.sendDone(message_t* bufPtr, error_t error) {
    if (&packet == bufPtr) {
      locked = FALSE;
    }
  }

}





